﻿using Dapper;
using Dapper.Contrib.Extensions;
using Remember.Model;
using System;
using System.Collections.Generic;

namespace Remember.Persistence.DapperNet
{
    /// <summary>
    /// 具体的CRUD操作实现，如果有其它特殊需求，则需要重新定义相应的接口，然后具体的业务类通过实现多接口的方式来实现复杂的查询
    /// </summary>
    public class TaskUserRepository : IRepository<TaskUser>
    {
        /// <summary>
        /// 用于管理DbConnection的类
        /// </summary>
        readonly DbSession _session;

        /// <summary>
        /// 用于Autofac注入
        /// </summary>
        /// <param name="session"></param>
        public TaskUserRepository(DbSession session)
        {
            if (session == null)
                throw new ArgumentNullException("session");

            _session = session;
        }

        /// <summary>
        /// 插入新数据
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Add(TaskUser item)
        {
            return _session.DbConnection.Execute(@"insert into dbo.TaskUser(UserName,Email,Age,Address)values(@UserName,@Email,@Age,@Address)",
                        new
                        {
                            @UserName = item.UserName,
                            @Email = item.Email,
                            @Age = item.Age,
                            @Address = item.Address
                        });
        }

        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Update(TaskUser item)
        {
            return _session.DbConnection.Execute(@"update dbo.TaskUser set UserName = @UserName,Email = @Email,Age = @Age,Address = @Address where UserId = @Id",
                       new
                       {
                           @Id = item.Id,
                           @UserName = item.UserName,
                           @Email = item.Email,
                           @Age = item.Age,
                           @Address = item.Address
                       });
        }

        /// <summary>
        /// 获取某条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TaskUser FindById(int id)
        {
            Dapper.SqlMapper.SetTypeMap(typeof(TaskUser), new ColumnAttributeTypeMapper<TaskUser>());

            return _session.DbConnection.QueryFirstOrDefault<TaskUser>("select * from dbo.TaskUser where UserId = @Id", new { @Id = id });
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <returns></returns>
        public IEnumerable<TaskUser> FindBySpecification()
        {
            Dapper.SqlMapper.SetTypeMap(typeof(TaskUser), new ColumnAttributeTypeMapper<TaskUser>());

            return _session.DbConnection.Query<TaskUser>("select * from dbo.TaskUser");
        }

        /// <summary>
        /// 查询分页数据
        /// </summary>
        /// <param name="take"></param>
        /// <param name="skip"></param>
        /// <returns></returns>
        public IEnumerable<TaskUser> FindBySpecification(int take, int skip)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 删除指定数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Remove(int id)
        {
            return _session.DbConnection.Execute("delete from dbo.TaskUser where UserId = @Id", new { Id = id });
        }

        /// <summary>
        /// 删除指定数据
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Remove(TaskUser item)
        {
            return Remove(item.Id);
        }
    }
}
